//
//  LSIMPlatform.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/26.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LSIMPlatform.h"
#import "LSUserModel.h"

@interface LSIMPlatform()
@end
@implementation LSIMPlatform

//初始化IMSDK
- (void)initIMSDK {
    [[TIMManager sharedInstance] setEnv:0]; // 0 正式环境（默认） 1 测试环境
    [[TIMManager sharedInstance] initSdk:IMAPPID accountType:IMACCOUNTTYPE];
    [[TIMManager sharedInstance] setLogLevel:TIM_LOG_NONE];
    [[TIMManager sharedInstance] disableCrashReport];
    [[TIMManager sharedInstance] setUserStatusListener:self];

}

//发送请求获取usersig
- (void)prepareIMLogSuccess:(Success)success Failed:(Failed)failed {
    NSDictionary *dic = @{};
    NSString *urlString = [TEST_URL stringByAppendingString:@""];
//    __weak typeof(self)WeakSelf = self;
    [NetworkTools postReqeustWithParams:dic UrlString:urlString successBlock:^(id responseObject) {
        NSMutableDictionary *IMParaDic = [NSMutableDictionary dictionaryWithCapacity:1];
        [IMParaDic setObject:IMACCOUNTTYPE forKey:@"accountType"];
        [IMParaDic setObject:@"" forKey:@"identifier"];
        [IMParaDic setObject:@"" forKey:@"userSig"];
        [IMParaDic setObject:KNum(IMAPPID) forKey:@"appidAt3rd"];
        [IMParaDic setObject:@(IMAPPID) forKey:@"sdkAppId"];

//        LSUserModel *userModel = [[LSUserModel alloc] initWithDict:IMParaDic];
//        [WeakSelf loginIMSDKWithIMUserModel:userModel];
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

//登录IMSDK
- (void)loginIMSDKWithIMUserModelWithSucc:(TIMLoginSucc)succ Fail:(TIMFail)fail {
    [self initIMSDK];
    TIMLoginParam *param = [[TIMLoginParam alloc] init];
    param.identifier = KString(User_ID);
    param.accountType = IMACCOUNTTYPE;
    param.appidAt3rd = KNum(IMAPPID);
    param.sdkAppId = IMAPPID;
    param.userSig = [[LSUserInfoManager sharedManager] getUserSign];
    
    [[TIMManager sharedInstance] login:param succ:^{
        NSLog(@"LoginSuccess!");
        if (succ) {
            succ();
        }
    } fail:^(int code, NSString *msg) {
        NSLog(@"LoginFailed -> %d %@",code,msg);
        if (fail) {
            fail(code,msg);
        }
    }];
}

//退出IM登录
- (void)logout {
    [[TIMManager sharedInstance] logout:^() {
//   在需要切换帐号时，需要logout回调成功或者失败后才能再次login，否则login可能会失败。
        NSLog(@"logout succ");
    } fail:^(int code, NSString * err) {
        NSLog(@"logout fail: code=%d err=%@", code, err);
    }];
}


@end
